package com.thjk.bdf.preloan.service.controller;

import com.thjk.bdf.common.base.BasicResp;
import com.thjk.bdf.common.enmus.AdmissionEnum;
import com.thjk.bdf.common.entity.CheckFailLog;
import com.thjk.bdf.common.utils.Constants;
import com.thjk.bdf.common.utils.RespUtils;
import com.thjk.bdf.preloan.service.domain.domainservice.CheckFailLogService;
import com.thjk.bdf.preloan.service.domain.domainservice.InvoiceAdmittanceService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/invoice/")
public class InvoiceAdmittanceController {

    private static final Logger logger = LoggerFactory.getLogger(InvoiceAdmittanceController.class);

    @Autowired
    InvoiceAdmittanceService invoiceAdmittanceService;

    @Autowired
    private CheckFailLogService checkFailLogService;

    @RequestMapping(value = "admittance/{creditCode}", method = RequestMethod.GET)
    public BasicResp invoiceAdmittance(@PathVariable(name = "creditCode") String creditCode) {
        logger.info("白名单企业额度预查, creditCode:{}", creditCode);

        BasicResp resp = null;
        try {
//            AdmissionEnum result = invoiceAdmittanceService.inspectInvoice(creditCode);
//
//            resp = RespUtils.buildFromAdmissionEnum(result);
        } catch (Exception e) {
            logger.error("额度预查异常", e);
            resp = RespUtils.BASIC_ERROR_RESP;
        }

        CheckFailLog checkFailLog = buildCheckFailLog(creditCode, resp);
        checkFailLogService.insertCheckFailLog(checkFailLog);

        logger.info("白名单企业额度预查, creditCode:{}, resp:{}", creditCode, resp);
        return resp;
    }

    /**
     * 保存不准入信息
     */
    public CheckFailLog buildCheckFailLog(String creditCode, BasicResp resp) {
        CheckFailLog checkFailLog = new CheckFailLog();

        checkFailLog.setRegisterNo(creditCode);
        checkFailLog.setCheckType(Constants.REFUSE_TONGHUA);
        checkFailLog.setResult(AdmissionEnum.getInnerDescByCode(resp.getCode()));
        checkFailLog.setFailCode(resp.getCode());

        return checkFailLog;
    }
}
